package cn.gdchent.springbootsecurity.controller;

import cn.gdchent.springbootsecurity.enity.UserInfoDetails;
import cn.gdchent.springbootsecurity.form.LoginForm;
import cn.gdchent.springbootsecurity.form.Regform;
import cn.gdchent.springbootsecurity.response.HttpResultMap;
import cn.gdchent.springbootsecurity.service.LoginService;
import cn.gdchent.springbootsecurity.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @auther:gdchent
 * @create:2020/4/17 11:09
 * @Description:
 */
@Controller
public class HomeController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private LoginService loginService;

    @RequestMapping({"/", "/index"})
    public String index() {
        System.out.println("第一步 走控制器首页方法");
        return "index";
    }

    @RequestMapping(path = {"/login"})
    public String login(Model model) {
        System.out.println("登录方法被调用");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Collection<GrantedAuthority> authorityCollection = (Collection<GrantedAuthority>) authentication.getAuthorities();
            System.out.println("list:" + authentication.toString());
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            model.addAttribute("username", name);
            model.addAttribute("authorities", authorityCollection.toString());
        }
        return "login";
    }

    /**
     * 注册用户
     *
     * @param regform
     * @return
     */
    @RequestMapping(path = {"/register"}, method = RequestMethod.POST)
    @ResponseBody
    public HttpResultMap register(@RequestBody Regform regform) {
        //添加用户角色
        UserInfoDetails userInfoDetails = new UserInfoDetails();
        userInfoDetails.setUsername(regform.getUserName());
        userInfoDetails.setPassword(regform.getPassword());
        Collection<GrantedAuthority> authorityCollection=new ArrayList<>();
        authorityCollection.add(new SimpleGrantedAuthority("ROLE_USER"));
        userInfoDetails.setAuthorities(authorityCollection);
        System.out.println("regForm" + regform);
        return registerService.register(userInfoDetails);

    }

    @PostMapping(path = "/doLogin")
    @ResponseBody
    public HttpResultMap doLogin(@RequestBody LoginForm loginForm) {
        return loginService.login(loginForm);

    }

    @GetMapping(path = {"/loginError"})
    public String loginError() {
        System.out.println("loginError方法被调用");
        return "error/loginError";
    }

    @GetMapping(path = {"/logoutUrl"})
    public void logoutUrl() {
        System.out.println("logoutUrl_controller");
    }

    @GetMapping(path = {"/logoutSuccessUrl"})
    public void logoutSuccessUrl() {
        System.out.println("logoutSuccessUrl_controller");
    }

}
